﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Tilver.DBConnectionUI;
namespace Tilver.DBConnectionUI.Postgre
{
    [ToolboxItem(false)]
    internal partial class PostgreEditor : UserControl,IUDLEditor,IOleDbEditor
    {
        PostgreStringBuilder builder = new PostgreStringBuilder();
        public PostgreEditor()
        {
            InitializeComponent();

            txServer.TextChanged += delegate { builder.Server = txServer.Text; };
            txPort.Validated+=delegate{builder.Port = Convert.ToInt32(txPort.Text);};
            txInitialCatalog.TextChanged += delegate { builder.Database = txInitialCatalog.Text; };
            txUserID.TextChanged += delegate { builder.UserID = txUserID.Text; };
            txPassword.TextChanged += delegate { builder.Password = txPassword.Text; };
            ckIntegratedSecurity.CheckedChanged += delegate { setSecurityMode(); };
            ckNameandPass.Checked = true;
            txPort.Validating+=new CancelEventHandler(ValidateInt32);
        }

        #region IConnectionEditor Members

        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(oleprov))
                    return builder.ConnectionString;
                else
                    return string.Format("Provider={0};{1}", oleprov, builder.ConnectionString);
            }
            set
            {
                builder.ConnectionString = value;
                RefreshData();
            }
        }

        public void RefreshData()
        {
            txServer.Text = builder.Server;
            txPort.Text = Convert.ToString(builder.Port);
            txInitialCatalog.Text = builder.Database;
            txUserID.Text = builder.UserID;
            txPassword.Text = builder.Password;
        }

        public object GetAdvancedPropertiesObject()
        {
            return builder;
        }

        #endregion

        #region helper methods

        private void setSecurityMode()
        {
            bool integrated = this.ckIntegratedSecurity.Checked;
            if (integrated)
            {
                builder.IntegratedSecurity = true;
                builder.UserID = "";
                builder.Password = "";
            }
            else
                builder.IntegratedSecurity = false;

            this.txUserID.Text = builder.UserID;
            this.txPassword.Text = builder.Password;

            this.txUserID.Enabled = !integrated;
            this.txPassword.Enabled = !integrated;
        }

        void ValidateInt32(object sender, CancelEventArgs e)
        {
            if (sender is TextBoxBase)
            {
                TextBoxBase txt = (TextBoxBase)sender;
                try
                {
                    Convert.ToInt32(txt.Text);
                    txt.ForeColor = SystemColors.ControlText;
                    e.Cancel = false;
                }
                catch (Exception)
                {
                    txt.ForeColor = Color.Red;
                    e.Cancel = true;
                }
            }
        } 
        #endregion

        #region IOleDbEditor Members

        string oleprov;
        public void SetOleProviderID(string invariantName)
        {
            oleprov = "PostgreSQL.1";
            builder.AddKeywordOverride("Server", "Data Source");
            builder.AddKeywordOverride("Database", "Location");
        }

        #endregion
    }
}
